package com.java1234.view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import com.java1234.dao.IBorrowDao;
import com.java1234.dao.IBorrowerDao;
import com.java1234.dao.IUserDao;
import com.java1234.dao.impl.BorrowImpl;
import com.java1234.dao.impl.BorrowerImpl;
import com.java1234.dao.impl.UserImpl;
import com.java1234.model.Borrower;
import com.java1234.util.DbUtil;
import com.java1234.util.StringUtil;

public class BorrowerManagerInterFrm extends JInternalFrame {
	private JTextField s_borrowerNameTxt;
	private JTable borrowerTable;
	private JTextField borrowerAccountTxt;
	private JTextField borrowerNameTxt;

	private DbUtil dbUtil = new DbUtil();
	private IBorrowerDao borrowerDao = new BorrowerImpl();
	private IBorrowDao borrowDao = new BorrowImpl();
	private IUserDao userDao = new UserImpl();
	private JTextField passwordTxt;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BorrowerManagerInterFrm frame = new BorrowerManagerInterFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BorrowerManagerInterFrm() {
		setIconifiable(true);
		setClosable(true);
		setTitle("借阅者信息");
		setBounds(100, 100, 928, 747);

		JLabel lblNewLabel = new JLabel("借阅者姓名：");

		s_borrowerNameTxt = new JTextField();
		s_borrowerNameTxt.setColumns(10);

		JButton btnNewButton = new JButton("查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				borrowerSearchActionPerformed(e);
			}
		});

		JScrollPane scrollPane = new JScrollPane();

		JScrollPane scrollPane_1 = new JScrollPane();

		JPanel panel = new JPanel();
		panel.setBorder(
				new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup()
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup().addGap(176).addComponent(lblNewLabel).addGap(52)
								.addComponent(s_borrowerNameTxt, GroupLayout.PREFERRED_SIZE, 219,
										GroupLayout.PREFERRED_SIZE)
								.addGap(75).addComponent(btnNewButton))
						.addGroup(groupLayout.createSequentialGroup().addGap(94)
								.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
										.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(scrollPane_1, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 708,
												GroupLayout.PREFERRED_SIZE))
								.addGap(54).addComponent(scrollPane, GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
				.addContainerGap(54, Short.MAX_VALUE)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup().addGap(73)
						.addGroup(groupLayout
								.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel)
								.addComponent(btnNewButton).addComponent(s_borrowerNameTxt, GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup().addGap(163).addComponent(scrollPane,
										GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup().addGap(87).addComponent(scrollPane_1,
										GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE)))
						.addGap(29).addComponent(panel, GroupLayout.PREFERRED_SIZE, 257, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(28, Short.MAX_VALUE)));

		JLabel lblNewLabel_1 = new JLabel("借阅者账号：");

		JLabel lblNewLabel_2 = new JLabel("借阅者姓名：");

		JButton btnNewButton_1 = new JButton("修改");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				borrowerUpdateActionEvent(e);
			}
		});

		JButton btnNewButton_2 = new JButton("删除");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				borrowerDeleteActionPerformed(e);
			}
		});

		borrowerAccountTxt = new JTextField();
		borrowerAccountTxt.setEditable(false);
		borrowerAccountTxt.setColumns(10);

		borrowerNameTxt = new JTextField();
		borrowerNameTxt.setColumns(10);

		JLabel lblNewLabel_3 = new JLabel("密码：");

		passwordTxt = new JTextField();
		passwordTxt.setColumns(10);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel
				.createSequentialGroup().addGap(29)
				.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup().addGap(13).addComponent(btnNewButton_1).addGap(59)
								.addComponent(btnNewButton_2))
						.addGroup(gl_panel.createSequentialGroup().addGroup(gl_panel
								.createParallelGroup(Alignment.TRAILING).addComponent(lblNewLabel_2)
								.addGroup(gl_panel.createParallelGroup(Alignment.LEADING).addComponent(lblNewLabel_1)
										.addComponent(lblNewLabel_3, Alignment.TRAILING)))
								.addGap(28)
								.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
										.addComponent(borrowerNameTxt).addComponent(passwordTxt, Alignment.LEADING)
										.addComponent(borrowerAccountTxt, Alignment.LEADING, GroupLayout.DEFAULT_SIZE,
												299, Short.MAX_VALUE))))
				.addContainerGap(250, Short.MAX_VALUE)));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel
						.createSequentialGroup().addGap(35)
						.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_1)
								.addComponent(borrowerAccountTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
						.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_2)
								.addComponent(borrowerNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE))
						.addGap(26)
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING).addComponent(lblNewLabel_3)
								.addComponent(passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(ComponentPlacement.UNRELATED, 32, Short.MAX_VALUE)
						.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(btnNewButton_2)
								.addComponent(btnNewButton_1))
						.addContainerGap()));
		panel.setLayout(gl_panel);

		borrowerTable = new JTable();
		borrowerTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				borrowerTableMousePressed(e);
			}
		});
		borrowerTable.setModel(new DefaultTableModel(new Object[][] {},
				new String[] { "\u501F\u9605\u8005\u8D26\u53F7", "\u501F\u9605\u8005\u59D3\u540D", "\u5BC6\u7801" }) {
			boolean[] columnEditables = new boolean[] { false, false, false };

			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		scrollPane_1.setViewportView(borrowerTable);
		getContentPane().setLayout(groupLayout);
		// 设置文本域边框
		borrowerAccountTxt.setBorder(new LineBorder(new java.awt.Color(127, 157, 185), 1, false));

		this.fillTable(new Borrower());
	}

	/**
	 * 初始化表格
	 * 
	 * @param bookType
	 */
	private void fillTable(Borrower borrower) {
		DefaultTableModel dtm = (DefaultTableModel) borrowerTable.getModel();
		dtm.setRowCount(0);// 设置成0行
		Connection con = null;
		try {
			con = dbUtil.getCon();
			ResultSet rs = borrowerDao.list(con, borrower);
			while (rs.next()) {
				Vector v = new Vector();
				v.add(rs.getString("borrowerAccount"));
				v.add(rs.getString("borrowerName"));
				v.add(rs.getString("password"));
				dtm.addRow(v);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 借阅者搜索事件处理
	 * 
	 * @param e
	 */
	private void borrowerSearchActionPerformed(ActionEvent evt) {
		
		String s_borrowerName = this.s_borrowerNameTxt.getText();
		Borrower borrower = new Borrower();
		borrower.setBorrowerName(s_borrowerName);
		this.fillTable(borrower);
	}

	/**
	 * 表格行点击事件处理
	 * 
	 * @param e
	 */
	private void borrowerTableMousePressed(MouseEvent evt) {
		int row = borrowerTable.getSelectedRow();
		borrowerAccountTxt.setText((String) borrowerTable.getValueAt(row, 0));
		borrowerNameTxt.setText((String) borrowerTable.getValueAt(row, 1));
		passwordTxt.setText((String) borrowerTable.getValueAt(row, 2));
	}

	/**
	 * 借阅者信息修改处理
	 * 
	 * @param evt
	 */
	private void borrowerUpdateActionEvent(ActionEvent evt) {
		String borrowerAccount = borrowerAccountTxt.getText();
		String borrowerName = borrowerNameTxt.getText();
		String password = passwordTxt.getText();
		if (StringUtil.isEmpty(borrowerAccount)) {
			JOptionPane.showMessageDialog(null, "请选择要修改的记录");
			return;
		}
		if (StringUtil.isEmpty(borrowerName)) {
			JOptionPane.showMessageDialog(null, "借阅者姓名不能为空");
			return;
		}
		Borrower borrower = new Borrower(borrowerAccount, borrowerName, password);
		Connection con = null;
		try {
			con = dbUtil.getCon();
			int modifyNum = borrowerDao.update(con, borrower);
			int n = userDao.updateBorrower(con, borrower);
			if (modifyNum == 1 && n == 1) {
				JOptionPane.showMessageDialog(null, "修改成功");
				this.resetValue();
				this.fillTable(new Borrower());
			} else {
				JOptionPane.showMessageDialog(null, "修改失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "修改失败");
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 借阅者删除事件处理
	 * 
	 * @param evt
	 */
	private void borrowerDeleteActionPerformed(ActionEvent evt) {
		String borrowerAccount = borrowerAccountTxt.getText();
		if (StringUtil.isEmpty(borrowerAccount)) {
			JOptionPane.showMessageDialog(null, "请选择要删除的记录");
			return;
		}
		int n = JOptionPane.showConfirmDialog(null, "确定要删除该记录吗？");
		if (n == 0) {
			Connection con = null;
			try {
				con = dbUtil.getCon();
				boolean flag = borrowDao.existBorrowByBorrower(con, borrowerAccount);
				if (flag) {
					JOptionPane.showMessageDialog(null, "当前借阅者信息下存在借阅记录，不能删除此人");
					return;
				}
				int deleteNum = borrowerDao.delete(con, borrowerAccount);
				int d = userDao.deleteBorrower(con, borrowerAccount);
				if (deleteNum == 1 && d == 1) {
					JOptionPane.showMessageDialog(null, "删除成功");
					this.resetValue();
					this.fillTable(new Borrower());
				} else {
					JOptionPane.showMessageDialog(null, "删除失败");
				}
			} catch (Exception e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "删除失败");
			} finally {
				try {
					dbUtil.closeCon(con);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}


	}

	/**
	 * 重置表单
	 */
	private void resetValue() {
		this.borrowerAccountTxt.setText("");
		this.borrowerNameTxt.setText("");
		this.passwordTxt.setText("");
	}
}
